package com.bruce.pullrefresh;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 *
 * Created by Administrator on 2015/8/21.
 */
public class ExRecyclerView extends RecyclerView {
    public ExRecyclerView(Context context) {
        super(context);
    }

    public ExRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setLayoutManager(final LayoutManager layoutManager) {
        super.setLayoutManager(layoutManager);
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager lm = (GridLayoutManager) layoutManager;
            lm.setSpanSizeLookup(new GridSpanSizeLookup(lm.getSpanCount()));
        } else if (layoutManager instanceof MyStaggeredGridLayoutManager) {
            MyStaggeredGridLayoutManager lm = (MyStaggeredGridLayoutManager) layoutManager;
            lm.setmSpanSizeLookup(new GridSpanSizeLookup(lm.getSpanCount()));
        }
    }


    /**
     * 设置Grid/StaggeredGrid LayoutManager的布局样式
     */
    private class GridSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {

        private int mSpanSize = 1;

        public GridSpanSizeLookup(int spanSize) {
            mSpanSize = spanSize;
        }

        @Override
        public int getSpanSize(int position) {
            BaseRecyclerAdapter adapter = (BaseRecyclerAdapter) getAdapter();
            if (adapter.getItemViewType(position) == BaseRecyclerAdapter.ITEM_HEADER) {
                return mSpanSize;
            }
            return 1;
        }
    }

}
